#include <stdbool.h>


#define ElemType int

typedef struct DNode{
    ElemType data;
    struct DNode *next;
    struct DNode *prior;
}DNode;

typedef struct DHead{
    ElemType length;
    struct DNode *next;
    struct DNode *prior;
}DHead, *DLinkList;

void printDLinkList(DLinkList DL); // 打印双链表的信息
DLinkList initDLinkList(); // 初始化双链表
bool DLHeadInsert(DLinkList DL, ElemType value); // 使用头插法往双链表中插入元素

void printDLinkList(DLinkList DL)
{
    DNode *p = DL->next;
    printf("length: %d\n", DL->length);
    printf("data: \n");
    int count = 1;
    while ( p != NULL)
    {
        printf("%d ", p->data);
        if(count % 10 == 0)
        {
            printf("\n");
        }
        count++;
        p = p->next;
    }
}

DLinkList initDLinkList()
{
    DLinkList DL = NULL;
    DL = (DLinkList) malloc(sizeof(DLinkList));
    if(DL == NULL)
    {
        printf("双链表空间分配失败\n");
        return NULL;
    }
    DL->length = 0;
    DL->next = NULL;
    DL->prior = NULL;
    return DL;
}

bool DLHeadInsert(DLinkList DL, ElemType value)
{
    if(DL == NULL)
    {
        printf("DL双链表指针为空\n");
        return false;
    }
    DNode* dnode = (DNode*) malloc(sizeof(DNode));
    if (dnode == NULL)
    {
        printf("数据节点创建失败\n");
        return false;
    }
    dnode->prior = DL;
    dnode->next = DL->next;
    dnode->next->prior = dnode;
    DL->next = dnode;
    return true;
}



